import java.sql.SQLException;

public class StudentService {
    private StudentDao studentDao = new StudentDao();


    public void register(String account, String password, String name) throws SQLException {
        if (account == null || account.trim().isEmpty()) {
            throw new IllegalArgumentException("账号不能为空！");
        }
        if (password == null || password.trim().isEmpty()) {
            throw new IllegalArgumentException("密码不能为空！");
        }
        if (name == null || name.trim().isEmpty()) {
            throw new IllegalArgumentException("姓名不能为空！");
        }
        studentDao.register(new Student(account, password, name));
    }


    public Student login(String account, String password) throws SQLException {
        if (account == null || account.trim().isEmpty() || password == null || password.trim().isEmpty()) {
            throw new IllegalArgumentException("账号或密码不能为空！");
        }
        return studentDao.login(account, password);
    }


    public void updatePassword(String account, String oldPassword, String newPassword) throws SQLException {
        // 1. 校验新密码不为空
        if (newPassword == null || newPassword.trim().isEmpty()) {
            throw new IllegalArgumentException("新密码不能为空！");
        }
        // 2. 校验原密码正确（先登录验证）
        Student student = login(account, oldPassword);
        if (student == null) {
            throw new IllegalArgumentException("原密码错误！");
        }
        // 3. 执行修改
        studentDao.updatePassword(account, newPassword);
    }
}